package com.frame.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frame.common.core.entity.SysConfig;
import com.frame.common.core.security.utils.SecurityUtils;
import com.frame.db.util.Condition;
import com.frame.db.util.Query;
import com.frame.stant.constans.UserConstants;
import com.frame.stant.utils.R;
import com.frame.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 参数配置 信息操作处理
 *
 * @author ruoyi
 */
@Api(tags ="系统参数配置")
@RestController
@RequestMapping("/system/config")
@AllArgsConstructor
public class SysConfigController {

    private final ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:config:list')")
    @GetMapping("/list")
    public R list(SysConfig config, Query query) {
        IPage<SysConfig> list = configService.selectConfigList(Condition.page(query),config);
        return R.success(list);
    }

//    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@custom.hasAnyAuthority('system:config:export')")
//    @GetMapping("/export")
//    public AjaxResult export(SysConfig config) {
//        List<SysConfig> list = configService.selectConfigList(config);
//        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
//        return util.exportExcel(list, "参数数据");
//    }

    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:config:query')")
    @GetMapping(value = "/{configId}")
    public R getInfo(@PathVariable Long configId) {
        return R.success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public R getConfigKey(@PathVariable String configKey) {
        return R.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:config:add')")
//    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
//    @RepeatSubmit
    public R add(@Validated @RequestBody SysConfig config) {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
            return R.error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(SecurityUtils.getUsername());
        return R.success(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:config:edit')")
//    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysConfig config) {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config))) {
            return R.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getUsername());
        return R.success(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:config:remove')")
//    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public R remove(@PathVariable Long[] configIds) {
        return R.success(configService.deleteConfigByIds(configIds));
    }

    /**
     * 清空缓存
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:config:remove')")
//    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public R clearCache() {
        configService.clearCache();
        return R.success();
    }
}
